using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Xml;
using Channelvn.Cached.Common;

namespace Rewrite.UrlRewrite
{
    public class MetaRules
    {
        private List<MetaRule> objMetaRules;
        public struct MetaRule
        {
            public string CatId, Title, Kwd, Desc;
            public MetaRule(string catId, string title, string kwd, string desc)
            {
                this.CatId = catId;
                this.Title = title;
                this.Kwd = kwd;
                this.Desc = desc;
            }
        }
        public MetaRules()
        {
            string cacheName = "MetaforSEORule";
            objMetaRules = HttpContext.Current.Cache[cacheName] as List<MetaRule>;
            if (objMetaRules == null)
            {
                objMetaRules = new List<MetaRule>();
                try
                {
                    string configFilePath = HttpContext.Current.Server.MapPath("/Config/Meta.config");
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(configFilePath);
                    XmlNodeList nlstRules = xmlDoc.DocumentElement.SelectNodes("//rules/rule");

                    for (int i = 0; i < nlstRules.Count; i++)
                    {
                        MetaRule rule = new MetaRule();
                        rule.CatId = nlstRules[i].SelectSingleNode("Cat_ID") != null ? nlstRules[i].SelectSingleNode("Cat_ID").InnerText : "";
                        rule.Title = nlstRules[i].SelectSingleNode("Title") != null ? nlstRules[i].SelectSingleNode("Title").InnerText : "";
                        rule.Kwd = nlstRules[i].SelectSingleNode("Keywords") != null ? nlstRules[i].SelectSingleNode("Keywords").InnerText : "";
                        rule.Desc = nlstRules[i].SelectSingleNode("Description") != null ? nlstRules[i].SelectSingleNode("Description").InnerText : "";
                        objMetaRules.Add(rule);
                    }

                    XmlNode nodeFileSettingCacheExpire = xmlDoc.DocumentElement.SelectSingleNode("//Configuration/MetaRulesFile");
                    long fileSettingCacheExpire = Lib.Object2Long(nodeFileSettingCacheExpire.Attributes["cacheExpire"].Value);
                    if (fileSettingCacheExpire <= 0)
                    {
                        fileSettingCacheExpire = 3600;
                    }

                    CacheDependency fileDependency = new CacheDependency(configFilePath);
                    HttpContext.Current.Cache.Insert(cacheName, objMetaRules, fileDependency, DateTime.Now.AddSeconds(fileSettingCacheExpire), TimeSpan.Zero, CacheItemPriority.Normal, null);

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

        }
        public List<MetaRule> GetMathMeta(string catId)
        {
            string CacheName = "GetMathMeta_" + catId;
            List<MetaRule> lstObj;

            lstObj = HttpContext.Current.Cache[CacheName] as List<MetaRule>;
            if (lstObj == null)
            {
                lstObj = new List<MetaRule>();
                if (objMetaRules != null && objMetaRules.Count > 0)
                {
                    for (int i = 0; i < objMetaRules.Count; i++)
                    {
                        MetaRule rule = (MetaRule)objMetaRules[i];
                        if (rule.CatId == catId)
                        {
                            lstObj.Add(new MetaRule(rule.CatId, rule.Title, rule.Kwd, rule.Desc));
                        }
                    }
                }
                if (lstObj.Count == 0)
                {
                    MetaRule rule = (MetaRule)objMetaRules[0];
                    lstObj.Add(new MetaRule(rule.CatId, rule.Title, rule.Kwd, rule.Desc));
                }
                HttpContext.Current.Cache.Insert(CacheName, lstObj, new CacheDependency(HttpContext.Current.Server.MapPath("/Config/Meta.config")), DateTime.Now.AddSeconds(86400), TimeSpan.Zero, CacheItemPriority.Normal, null);
            }
            return lstObj;
        }
        public MetaRule this[int index]
        {
            get
            {
                return (MetaRule)objMetaRules[index];
            }
        }
    }
}
